﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace COM
{
    public class Common
    {
        public static string IDGenerator(string prefix)
        {
            DateTime currentTime = DateTime.Now;
            if (prefix.Length > CONST.PREFIX.PREFIX_LENGTH) prefix = prefix.Substring(0, CONST.PREFIX.PREFIX_LENGTH);
            string Id = prefix + "_" +
                intToString(currentTime.Year, 3) +
                intToString(currentTime.Month, 2) +
                intToString(currentTime.Day, 2) +
                intToString(currentTime.Hour, 2) +
                intToString(currentTime.Minute, 2) +
                intToString(currentTime.Second, 2) +
                intToString(currentTime.Millisecond, 2);
            return Id;
        }
        private static string intToString(int current, int NoOfStr)
        {
            string str = "00000" + current.ToString();
            str = str.Substring(str.Length - NoOfStr);
            return str;
        }
        public static string ToUnsignedString(string s)
        {
            Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string temp = s.Normalize(NormalizationForm.FormD);
            return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }
        private static bool isSignedString(string s)
        {
            string[] signedArr = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",  
    "đ",  
    "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
    "í","ì","ỉ","ĩ","ị",  
    "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
    "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
    "ý","ỳ","ỷ","ỹ","ỵ"};
            for (int i = 0; i < signedArr.Length; i++)
            {
                if (s.Contains(signedArr[i])) return true;
            }
            return false;

        }
        public static List<String> AddUnsignedAddress(List<String> address)
        {
            List<String> unsignedAddr = new List<String>();
            if (address == null)
            {
                return null;
            }
            else
            {
                for (int i = 0; i < address.Count; i++)
                {
                    if (isSignedString(address.ElementAt(i)))
                    {
                        String signedString = ToUnsignedString(address.ElementAt(i));
                        unsignedAddr.Add(signedString);
                    }
                }
            }
            address.AddRange(unsignedAddr);
            return address;
        }
    }
}
